package com.wangshb.upms.controller;

import com.wangshb.upms.core.tree.TreeNode;
import com.wangshb.upms.dto.upms.userRole.UpmsAllocateRoleRequestDto;
import com.wangshb.upms.dto.upms.userRole.UpmsUserRoleGetRequestDto;
import com.wangshb.upms.dto.upms.userRole.UpmsUserRoleGetResponseDto;
import com.wangshb.upms.service.UpmsRoleService;
import com.wangshb.upms.service.UpmsUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author auto code
 */
@RestController
public class UpmsUserRoleController {

    @Autowired
    private UpmsUserRoleService upmsUserRoleService;

    @Autowired
    private UpmsRoleService upmsRoleService;


    @PostMapping(value = "/upms/user/role/get")
    public UpmsUserRoleGetResponseDto treeRoleByUserId(@RequestBody UpmsUserRoleGetRequestDto getRequestDto) {

        TreeNode treeNode = TreeNode.listToTree(this.upmsRoleService.findForRoleTree());
        List<Long> checkedRoleIds = this.upmsUserRoleService.listRoleByUserId(getRequestDto.getUpmsUserId());
        return UpmsUserRoleGetResponseDto.builder().checkedKeys(checkedRoleIds).treeNode(treeNode).build();
    }


    @PostMapping(value = "/upms/user/role/allocate")
    public void allocateRoleByUserId(@RequestBody UpmsAllocateRoleRequestDto allocateRoleRequestDto) {
        this.upmsUserRoleService.allocateRoles(allocateRoleRequestDto.getUpmsUserId(), allocateRoleRequestDto.getRoleIdList());
    }
}
